package com.android.lmy.SqlLiteDemo;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;


/**
 * @功能:
 * @Creat 2020/1/8 9:52
 * @User Lmy
 * @Compony JinAnChang
 */
public class StudentDao {
    private static StudentDao INSTANCE;
    private DatabaseHelper mHelper;
    private SQLiteDatabase mDB;
    public String TABLE_NAME = "Student";//表的名称

    private StudentDao(Context context) {
        if (mHelper == null) {
            mHelper = new DatabaseHelper(context);
            mDB = mHelper.getWritableDatabase();
            String sql = "create table if not exists " + TABLE_NAME + " (studentid text primary key, name text, sex text, age integer)";
            mDB.execSQL(sql);

            //第一次调用创建数据库
        }
    }

    public static StudentDao getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = new StudentDao(context);
        }
        return INSTANCE;
    }

    /**
     * 添加数据
     */
    public void addstudent(Student student) {
        mDB.beginTransaction();
        ContentValues contentValues = new ContentValues();
        contentValues.put("studentid", student.getStudentid());
        contentValues.put("name", student.getName());
        contentValues.put("sex", student.getSex());
        contentValues.put("age", student.getAge());
        mDB.insertOrThrow(TABLE_NAME, null, contentValues);
        mDB.setTransactionSuccessful();
        mDB.endTransaction();//执行完成需要设置结束事务
    }


    /**
     * 删除数据
     */
    public int deletestudent(String selection, String[] selectionArgs) {
        int type;
        mDB.beginTransaction();
        type = mDB.delete(TABLE_NAME,
                selection, selectionArgs);
        mDB.setTransactionSuccessful();
        mDB.endTransaction();
        return type;
    }

    /**
     * 修改数据
     */
    public int updatestudent(ContentValues contentValues, String id) {
        mDB.beginTransaction();
        int type =
                mDB.update(TABLE_NAME,
                        contentValues,
                        "studentid = ?",
                        new String[]{id});
        mDB.setTransactionSuccessful();
        mDB.endTransaction();
        return type;
    }

    /**
     * 查询数据
     */
    public List<Student> getstudentlist() {
        Cursor cursor = mDB.rawQuery("select * from " + TABLE_NAME, null);
        if (cursor.getCount() > 0) {
            List<Student> studentList = new ArrayList<Student>(cursor.getCount());
            while (cursor.moveToNext()) {
                Student student = new Student(
                        cursor.getString(cursor.getColumnIndex("studentid")),
                        cursor.getString(cursor.getColumnIndex("name")),
                        cursor.getString(cursor.getColumnIndex("sex")),
                        cursor.getInt(cursor.getColumnIndex("age"))
                );
                studentList.add(student);
            }
            cursor.close();
            return studentList;
        }
        return null;
    }


    /**
     * 查询数据
     */
    public Cursor getStudentCursor() {
        Cursor cursor = mDB.rawQuery("select * from " + TABLE_NAME, null);
        return cursor;
    }


    /**
     * 添加数据
     */
    public void addstudentCursor(ContentValues contentValues) {
        mDB.beginTransaction();
        long type = mDB.insertOrThrow(TABLE_NAME, null, contentValues);
        mDB.setTransactionSuccessful();
        mDB.endTransaction();//执行完成需要设置结束事务
        Log.e("TTTSSS", "" + type);
    }

}
